# Contributor: Eric Molitor <eric@molitor.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Rasmus Thomsen <oss@cogitri.dev>
pkgname=llvm-libunwind
# Note: Update together with llvm.
pkgver=14.0.6
_llvmver=${pkgver%%.*}
pkgrel=0
pkgdesc="LLVM version of libunwind library"
url="https://llvm.org/"
arch="all !s390x"
license="Apache-2.0"
depends_dev="!libunwind-dev"
makedepends="cmake llvm$_llvmver-dev ninja"
subpackages="$pkgname-static $pkgname-dev"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/libunwind-$pkgver.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/llvm-$pkgver.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-$pkgver/libcxx-$pkgver.src.tar.xz
	libunwind-link-libssp.patch
	libcxx-musl.patch
	libcxx-ppc.patch
	"
builddir="$srcdir"
options="!check"

prepare() {
	cd "$srcdir"
	mv libunwind-$pkgver.src libunwind
	mv libcxx-$pkgver.src libcxx
	mv llvm-$pkgver.src llvm

	default_prepare
}

build() {
	local crossopts=''
	[ "$CBUILD" != "$CHOST" ] && crossopts="
		-DCMAKE_SYSTEM_NAME=Linux
		-DCMAKE_HOST_SYSTEM_NAME=Linux
		-DLIBUNWIND_SYSROOT=$CBUILDROOT"

	cmake -B build -G Ninja -S libunwind \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIBUNWIND_HAS_NODEFAULTLIBS_FLAG=OFF \
		-DLIBUNWIND_STANDALONE_BUILD=ON \
		$crossopts
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	mkdir -p "$pkgdir"/usr/include/mach-o
	cp libunwind/include/*.h "$pkgdir"/usr/include/
	cp libunwind/include/mach-o/*.h "$pkgdir"/usr/include/mach-o/
}

sha512sums="
c8f3804c47ac33273238899e5682f9cb52465dcceff0e0ecf9925469620c6c9a62cc2c708a35a0e156b666e1198df52c5fff1da9d5ee3194605dfd62c296b058  libunwind-14.0.6.src.tar.xz
6461bdde27aac17fa44c3e99a85ec47ffb181d0d4e5c3ef1c4286a59583e3b0c51af3c8081a300f45b99524340773a3011380059e3b3a571c3b0a8733e96fc1d  llvm-14.0.6.src.tar.xz
c39bb01317e8ae7e5b23a677754a866953b6c45188c194d3d76fbd276de9565df8bcbcab8cf6606584af7dd30a61f39376c0e2de6b8c32c69f7fc592587e710d  libcxx-14.0.6.src.tar.xz
9aa2a771fcf5de441a5386f5e0f6436bdc2fdf888ee2318487d9af0883cbc4fe20faa4b10f942e04b44d900a47934b7c40b916a16a4b0c0240e4d20d93c95bfb  libunwind-link-libssp.patch
e06a4ebc9ed4258729320d3d67fa0f7c9962c2435e713edda2fcbd9dca93bf85072b99ae4bdaa848254ace8423c7e20b9f829a3238eb4a4f5c5758ef4ee379f6  libcxx-musl.patch
fe6336bc73b3ee3aa6665b4a2a53f7360a1b23097002bb396c8515c4343e7bf8e84728f769aa033400e386c2c80a538c970f16719518040f4fea0fd667af523a  libcxx-ppc.patch
"
